package com.ling.tts3.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class PermissionUtils {
    private static final int MY_PERMISSIONS_REQUEST_RECORD_AUDIO = 100; // 权限返回：100：授权
    // tts 应用权限组

    private String [] permissions = {
            Manifest.permission.RECORD_AUDIO, // 录音权限
            Manifest.permission.WRITE_EXTERNAL_STORAGE, // 读写权限
            Manifest.permission.READ_EXTERNAL_STORAGE, // 读写权限
            Manifest.permission.INTERNET // 网络权限
    };

    /**
     *
     * @param context
     * @param activity
     */
    private void checkAndRequestPermissions(Context context, Activity activity) {
        List<String> needRequestPermissions = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                needRequestPermissions.add(permission);
            }
        }
        if (!needRequestPermissions.isEmpty()) {
            ActivityCompat.requestPermissions(activity,
                    needRequestPermissions.toArray(new String[0]),
                    MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
        } else {
            // 已经授权，执行相关操作
            // 这里可以执行需要授权的操作，例如开始录音或播放音频
        }
    }

}
